<?php
if(!function_exists('tagRandColor')) {
	function tagRandColor() {
		$letter = 'ABCDEF0123456789';
		$rst = array();
		for($i = 0; $i < 6; $i++) {
			$rst[$i] = substr($letter, rand(0, strlen($letter) - 1), 1);
			if(is_numeric($rst[$i]) && intval($rst[$i]) < 3) {
				$rst[$i] = 3;
			}
		}
		return implode('', $rst);
	}
}
if(!function_exists('tagFontSize')) {
	function tagFontSize($tags) {
		if(empty($tags)) return array();
		$largest = 22;
		$smallest = 8;
		
		$counts = array();
		$real_counts = array();
		foreach((array) $tags as $key => $tag) {
			$real_counts[$key] = $tag['stotal'];
			$counts[$key] = ($tag['stotal'] + $smallest) * $smallest;
		}
		
		$min_count = min($counts);
		$spread = max($counts) - $min_count;
		if($spread <= 0) $spread = 1;
		$font_spread = $largest - $smallest;
		if($font_spread < 0) $font_spread = 1;
		$font_step = $font_spread / $spread;
		
		$arr = array();
		foreach($tags as $key => $tag) {
			$size = round($smallest + (($counts[$key] - $min_count) * $font_step), 2);
			$arr[$key] = array_merge($tag, array('size' => $size));
		}
		return $arr;
	}
}
?>
<div class="tagcloud">
<?php 
	foreach(tagFontSize((array) $this->cache()->sidebar()->getSidebarTags(array('limit' => '40'))) AS $row) {
		echo '<a title="' . $row['stotal'] . ' special(s) total" href="';
		echo $row['_url'];
		echo '" style="font-size:' . $row['size'] . 'pt;color:#' . tagRandColor() . '">' . $this->escape($row['name']) . '</a>';
	}
	echo "\r\n";
?>
</div>